#include <iostream>
#include "Settings.h"
#include "TaskManager.h"
#include "OpenGLRenderer.h"
#include "Library.h"
#include "PhysicsTask.h"
#include "RenderTask.h"
#include "GlobalTimer.h"

#include "SceneGraph.h"
#include "LoadTGA.h"
#include "EditorGUIInit.h"
#include "MdlFileLoader.h"
#include "ObjModelFile.h"
#include "ModelFactory.h"

void InitializeRenderSettings()
{
	Settings::getSingleton().RegisterVariable("ShadeModel", new CDator<unsigned int>(Renderer->getShadeModel()));
	Settings::getSingleton().RegisterVariable("ClearColor", new CDatorVector<float>(Renderer->getClearColor()));
	Settings::getSingleton().RegisterVariable("ClearDepth", new CDator<float>(Renderer->getClearDepthValue()));
	Settings::getSingleton().RegisterVariable("DepthTest", new CDator<bool>(Renderer->getDepthTest()));
	Settings::getSingleton().RegisterVariable("DepthFunction", new CDator<unsigned int>(Renderer->getDepthFunc()));
	Settings::getSingleton().RegisterVariable("Hints", new CDatorVector<unsigned int>(Renderer->getHints()));
	Settings::getSingleton().RegisterVariable("WindowTitle", new CDator<std::string>(Renderer->getWindowTitle()));
	Settings::getSingleton().RegisterVariable("WindowWidth", new CDator<unsigned int>(Renderer->getWindowWidth()));
	Settings::getSingleton().RegisterVariable("WindowHeight", new CDator<unsigned int>(Renderer->getWindowHeight()));

	
	//Settings::getSingleton().RegisterVariable("FirstAbilityActivatorKey", new CDator<unsigned char>(ActiveGameState::FirstAbilityActivatorKey));
	//Settings::getSingleton().RegisterVariable("SecondAbilityActivatorKey", new CDator<unsigned char>(ActiveGameState::SecondAbilityActivatorKey));
	//Settings::getSingleton().RegisterVariable("ThirdAbilityActivatorKey", new CDator<unsigned char>(ActiveGameState::ThirdAbilityActivatorKey));
	//Settings::getSingleton().RegisterVariable("FourthAbilityActivatorKey", new CDator<unsigned char>(ActiveGameState::FourthAbilityActivatorKey));
	//Settings::getSingleton().RegisterVariable("UpKey", new CDator<unsigned char>(ActiveGameState::UpKey));
	//Settings::getSingleton().RegisterVariable("DownKey", new CDator<unsigned char>(ActiveGameState::DownKey));
	//Settings::getSingleton().RegisterVariable("MoveCameraForwardKey", new CDator<unsigned char>(char('w')));
	//Settings::getSingleton().RegisterVariable("MoveCameraBackwardKey", new CDator<unsigned char>((unsigned char)'s'));
	//Settings::getSingleton().RegisterVariable("MoveCameraLeftKey", new CDator<unsigned char>((unsigned char)'a'));
	//Settings::getSingleton().RegisterVariable("MoveCameraRightKey", new CDator<unsigned char>((unsigned char)'d'));
}

int main()
{
	new Settings();
	new OpenGLRenderer();
	new TaskManager();
	new Library();
	InitializeRenderSettings();

	Settings::getSingleton().ParseFile("Settings.txt");

	//Renderer->getWindowTitle() = "Bejan's Window";
	//Renderer->getWindowWidth() = 640;
	//Renderer->getWindowHeight() = 480;
	//Renderer->getClearDepthValue() = 1;
	//Renderer->getShadeModel() = 7425;
	//Renderer->getHints()[0] = 3152;
	//Renderer->getHints()[1] = 4354;
	//Renderer->getClearColor()[3] = 0.5f;
	//Renderer->getDepthTest() = true;
	//Renderer->getDepthFunc() = 515;
	
	Pointer<ITask> timer = Pointer<ITask>(new GlobalTimer());
	Pointer<ITask> physics = Pointer<ITask>(new PhysicsTask());

	TaskManager::getSingleton().AddTask(timer);
	TaskManager::getSingleton().AddTask(Pointer<ITask>(new RenderTask()));
	TaskManager::getSingleton().AddTask(physics);
	// TaskManager::getSingleton().AddTask(Pointer<ITask>(new InitializeTask()));
	
	Renderer->BuildFont(HashedString("Arial"), 10);
	
	MdlFile modelFile;
	ObjModelFile file;
	LoadObjModelFile((Settings::getSingleton().getDirectory() + "//Data//Models//sphere.obj").c_str(), file );
	ConvertToMdlFile(file, modelFile);
	modelFile.modelName = "Default";
	//LoadMdlFile(std::string(Settings::getSingleton().getDirectory() + "//Data//Models//cube.mdl"), modelFile );
	CreateModelFromMdl(modelFile);

	Pointer<Image> cheating = new Image();
	if (!LoadTGA(std::string(Settings::getSingleton().getDirectory() + "//Data//Terrain.tga"), *cheating))
		exit(5);
	Texture texture(cheating);
	
	EditorGUIInit(timer, physics);
	
	TaskManager::getSingleton().Execute();

	delete Library::getSingletonPtr();
	delete TaskManager::getSingletonPtr();
	delete Settings::getSingletonPtr();
};